/********************************** (C) COPYRIGHT *******************************
* File Name          : ch32v10x_it.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2020/04/30
* Description        : Main Interrupt Service Routines.
*******************************************************************************/
#include "ch32v10x_it.h"

void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void EXTI0_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void ADC1_2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
extern uint8_t exti0_status;
extern uint8_t DHT11_Updata;
extern uint8_t boot_time_ms;
extern uint8_t refre;
extern __IO uint16_t ADC_Value;
extern uint8_t count;
extern uint8_t MQ_ready;
/*******************************************************************************
* Function Name  : NMI_Handler
* Description    : This function handles NMI exception.
* Input          : None
* Return         : None
*******************************************************************************/
void NMI_Handler(void)
{
}

/*******************************************************************************
* Function Name  : HardFault_Handler
* Description    : This function handles Hard Fault exception.
* Input          : None
* Return         : None
*******************************************************************************/
void HardFault_Handler(void)
{
  while (1)
  {
  }
}

/*******************************************************************************
* Function Name  : EXTI0_IRQHandler
* Description    : This function handles EXTI0 Handler.
* Input          : None
* Return         : None
*******************************************************************************/
void EXTI0_IRQHandler(void)
{
  if(EXTI_GetITStatus(EXTI_Line0)!=RESET)
  {
    if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) != Bit_SET)
    {
        exti0_status = 1;
    }else
    {
        exti0_status = 0;
    }
    EXTI_ClearITPendingBit(EXTI_Line0);     /* Clear Flag */
  }
  if(EXTI_GetITStatus(EXTI_Line4)!=RESET)
    {

     if( GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4)!= Bit_SET)
     {

         GPIO_ResetBits(GPIOA,GPIO_Pin_3);
     }

     EXTI_ClearITPendingBit(EXTI_Line4);
    }
}

/*******************************************************************************
* Function Name  : TIM2_IRQHandler
* Description    : This function handles TIM2 Handler.
* Input          : None
* Return         : None
*******************************************************************************/
void TIM2_IRQHandler(void)
{
    if(TIM_GetFlagStatus(TIM2, TIM_IT_Update)!= RESET){
      refre = 1;
      DHT11_Updata ++;
      boot_time_ms ++;
      if(MQ_ready == 0){
          count++;
      }
      ADC_SoftwareStartConvCmd(ADC1, ENABLE);
    }
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}

/*******************************************************************************
* Function Name  : ADC1_2_IRQHandler
* Description    : This function handles analog wathdog exception.
* Input          : None
* Return         : None
*******************************************************************************/
void ADC1_2_IRQHandler(void)
{
    if(ADC_GetITStatus( ADC1, ADC_IT_AWD)){
        if(GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_2 | GPIO_Pin_3)!= Bit_SET){
        GPIO_SetBits(GPIOA,GPIO_Pin_2 | GPIO_Pin_3);
        }
    }
    if(ADC_Value<1500){
        GPIO_ResetBits(GPIOA,GPIO_Pin_2 | GPIO_Pin_3);
    }

    ADC_ClearITPendingBit( ADC1, ADC_IT_AWD);
}

